package com.nhjf.admin.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import com.nhjf.admin.common.util.SysProperties;
import com.nhjf.common.util.LogMessage;


public class ProductPicUtil  {
	String sysSeparator ;  // windows是\，unix是/
	String fileUpload;
	String tmpPath ;// 临时文件目录
	String shopPath; // 商品保存的目录地址
	String imageType;
	String activityPicturePath; //首页板块图片保存路径
	String upload;
	String specialTopicPicturePath; //专题活动图片保存路径
	String helpCategoryLogoPath; //帮助分类logo保存路径
	//zzq add
	String syTmpPath;//水印图片临时目录
	String syPath;//水印图片保存的目录
	
	String preTmpPath;
	
	public String getActivityPicturePath() {
		return activityPicturePath;
	}
	public void setActivityPicturePath(String activityPicturePath) {
		this.activityPicturePath = activityPicturePath;
	}
	public  ProductPicUtil(){
		  sysSeparator = File.separator; // windows是\，unix是/ 
		  upload= SysProperties.getInstance().get("fileUploadPath")+sysSeparator+"upload"+sysSeparator;
		  fileUpload =SysProperties.getInstance().get("fileUploadPath")+sysSeparator+ "fileUpload" + sysSeparator;
		  tmpPath = fileUpload + "tmp"; // 临时文件目录
		  shopPath = fileUpload + "shop"; // 商品保存的目录地址		
		  imageType = ".jpg";
		  //zzq add
		  syTmpPath = fileUpload + "sytmp";//水印图片的临时文件夹
		  syPath = fileUpload + "sypic";//水印图片目录
		  
		  preTmpPath = fileUpload + "preTmp";
		  
		  activityPicturePath=fileUpload + "activity"; //首页板块图片保存路径 
		  specialTopicPicturePath = fileUpload  + "specialTopic"; //专题活动图片保存路径
		  helpCategoryLogoPath = fileUpload  + "helpCategoryLogo"; //帮助分类logo保存路径
	}
	
	/** 组建保存图片路径 */
	public String getProductPicFilePath(Long prodId,String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("product").append(sysSeparator).append(prodId).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	/** 组建保存图片路径 */
	public String getLogoPicFilePath(Long prodId,String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("logo").append(sysSeparator).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	/** excl url */
	public String getExclFilePath(String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("excl").append(sysSeparator).append(sysSeparator).append(fileName);
		return picFilePath.toString();
		
	}
	/** qrcode  */
	public String getQrcodeFilePath(String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("qrcode").append(sysSeparator).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	/** qrcodeTemp  */
	public String getQrcodeTempFilePath(String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("qrcodeTemp").append(sysSeparator).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	/** qrcode zip  */
	public String getQrcodeZipFilePath(String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("qrcodezip").append(sysSeparator).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	public String getProductPath(Long prodId){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("product").append(sysSeparator).append(prodId);
        return picFilePath.toString();
    }
	
	
	/** 首页板块图片保存路径 zh*/
	public String getActivityPicturePath(Long prodId){
		StringBuffer activityFilePath= new StringBuffer();
		activityFilePath.append(activityPicturePath).append(sysSeparator).append(prodId);
        return activityFilePath.toString();
    }
	
	/**专题活动图片保存路径*/
	public String getSpecialTopicPicturePath(String specialTopicId){
		StringBuffer specialTopicFilePath = new StringBuffer();
		specialTopicFilePath.append(specialTopicPicturePath).append(sysSeparator).append(specialTopicId);
		return specialTopicFilePath.toString();
	}
	
	public String getHelpCategoryLogoPath(String helpCategoryId){
		StringBuffer helpCategoryFilePath = new StringBuffer();
		helpCategoryFilePath.append(helpCategoryLogoPath).append(sysSeparator).append(helpCategoryId);
		return helpCategoryFilePath.toString();
	}
	
	
	public String getWebRootProductPath(HttpServletRequest request ,Long prodId){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(SysProperties.getInstance().get("editimagePath")).append("/").append("shop").append("/").append("product").append("/").append(prodId);
        return picFilePath.toString();
	}
	
	public boolean mkdirForProduct(Long prodId){
		       boolean flag=true;
		       StringBuffer picFileDir= new StringBuffer();
		        picFileDir.append(shopPath).append(sysSeparator).append("product").append(sysSeparator).append(prodId);
                File fileDir = new File(picFileDir.toString());
				if (!fileDir.exists()) {// 存在该目录，则删除
				   flag = fileDir.mkdir();
				}
				return flag;
	}
	
	
	public boolean mkdirForPresent(String presentNo) {
		boolean flag = true;
		StringBuffer picFileDir = new StringBuffer();
		picFileDir.append(shopPath).append(sysSeparator).append("present")
				.append(sysSeparator).append(presentNo);
		File fileDir = new File(picFileDir.toString());
		if (!fileDir.exists()) {// 存在该目录，则删除
			flag = fileDir.mkdir();
		}
		return flag;
	}
	
	public String getPresentPicFilePath(String presentNo,String fileName){
		StringBuffer picFilePath= new StringBuffer();
		picFilePath.append(shopPath).append(sysSeparator).append("present").append(sysSeparator).append(presentNo).append(sysSeparator).append(fileName);
        return picFilePath.toString();
		
	}
	
	
	/**
	 * 删除图片文件夹
	 * @param f
	 * @return
	 */
	public boolean deletedir(File f) {
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()){
					deletedir(files[i]);
				}else{
					deletefile(files[i]);
				}
			}
		}
		f.delete();
		return true;

	}
	/**
	 * 删除文件
	 * @param f
	 * @return
	 */
	private boolean deletefile(File f) {
		if (f.isFile()) {
			f.delete();
		}
		return true;
	}
	/**
	 * 上传文件到临时文件夹里
	 * @param file       文件
	 * @param FileName   保存图片的路径
	 * @return
	 */
	public String uploadFileToTmp(File file,String FileName){
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(tmpPath).append(sysSeparator).append(FileName).toString();
		File fileObj=new File(imageTmpPath);
		fileObj.getParentFile().mkdirs();
		if(fileObj.exists()){
			fileObj.delete();
		}
	    try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(new File(imageTmpPath)));
			byte[] buffer = new byte[64 * 1024];
			int length;
			while ((length = in.read(buffer, 0, buffer.length)) > 0) {
				ou.write(buffer, 0, length);
			}
			in.close();
			ou.flush();
			ou.close();
		} catch (Exception e) {
			e.printStackTrace();
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
    /**
     * 
     * @方法功能说明：MultipartFile转File并存入临时文件夹
     * @修改者名字：郑奕凯
     * @修改时间：2017年8月2日 下午4:52:03
     * @修改内容：
     * @参数：
     * @return:新文件路径
     * @throws
     */
	public String uploadFileToTmp(MultipartFile file,String FileName){
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(tmpPath).append(sysSeparator).append(FileName).toString();
	    File fileObj = new File(imageTmpPath);  
		if(!fileObj.getParentFile().exists()) {
			fileObj.getParentFile().mkdirs();
		}
		if(fileObj.exists()){
			fileObj.delete();
		}
        //保存  
        try {  
        	file.transferTo(fileObj);
		} catch (Exception e) {
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
	
	/**
	 * 删除文件
	 * @param filePath
	 * @return
	 */
	public boolean deleteFile(String filePath){
		 boolean flag=true;
		try {
			File deleteFile = new File(filePath);
			deleteFile.delete();
		} catch (RuntimeException e) {
			flag=false;
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return flag;
	}
	
	/**
	 * 创建文件夹
	 * @param filePath
	 * @return
	 */
	public boolean createFile(String filePath){
		 boolean flag=true;
		 File file = new File(filePath);
			try {
				if (file.exists()) {
		             
		        } else {
		          
		           file.mkdir();
		        }
			} catch (RuntimeException e) {
				flag=false;
				LogMessage.logwrite(this.getClass(), e.getMessage());
			}
		 
		return flag;
	}
	
	public String getSysSeparator() {
		return sysSeparator;
	}
	public void setSysSeparator(String sysSeparator) {
		this.sysSeparator = sysSeparator;
	}
	public String getFileUpload() {
		return fileUpload;
	}
	public void setFileUpload(String fileUpload) {
		this.fileUpload = fileUpload;
	}
	public String getTmpPath() {
		return tmpPath;
	}
	public void setTmpPath(String tmpPath) {
		this.tmpPath = tmpPath;
	}
	public String getShopPath() {
		return shopPath;
	}
	public void setShopPath(String shopPath) {
		this.shopPath = shopPath;
	}
	public String getImageType() {
		return imageType;
	}
	public void setImageType(String imageType) {
		this.imageType = imageType;
	}
	public void setUpload(String upload) {
		this.upload = upload;
	}
	public String getUpload() {
		return upload;
	}
	
	//zzq add 2015-10-30
	public String getSyTmpPath() {
		return syTmpPath;
	}
	public void setSyTmpPath(String syTmpPath) {
		this.syTmpPath = syTmpPath;
	}
		
	public String getSyPath() {
		return syPath;
	}
	public void setSyPath(String syPath) {
		this.syPath = syPath;
	}
	public String getPreTmpPath() {
		return preTmpPath;
	}
	public void setPreTmpPath(String preTmpPath) {
		this.preTmpPath = preTmpPath;
	}
	/**
	 * 水印临时文件 zzq create
	 * @param file       文件
	 * @param FileName   文件名
	 * @return 文件的绝对路径
	 */
	public String uploadImgToFile(File file,String filePath,String FileName){
		if(filePath == null || FileName == null){
			return null;
		}
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(filePath).append(sysSeparator).append(FileName).toString();
		File fileObj=new File(imageTmpPath);
		if(!fileObj.getParentFile().exists()) {
			fileObj.getParentFile().mkdirs();
		}
		if(fileObj.exists()){
			fileObj.delete();
		}
	    try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			BufferedOutputStream ou = new BufferedOutputStream(new FileOutputStream(new File(imageTmpPath)));
			byte[] buffer = new byte[64 * 1024];
			int length;
			while ((length = in.read(buffer, 0, buffer.length)) > 0) {
				ou.write(buffer, 0, length);
			}
			in.close();
			ou.flush();
			ou.close();
		} catch (Exception e) {
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
	
	/**
	 * 
	 * @方法功能说明：MultipartFile转File并存入文件夹
	 * @修改者名字：郑奕凯
	 * @修改时间：2017年8月2日 下午4:44:51
	 * @修改内容：
	 * @参数：
	 * @return:
	 * @throws
	 */
	public String uploadImgToFile(MultipartFile file,String filePath,String FileName){
		if(filePath == null || FileName == null){
			return null;
		}
		StringBuffer sb=new StringBuffer();
	    String imageTmpPath=sb.append(filePath).append(sysSeparator).append(FileName).toString();
	    File fileObj = new File(imageTmpPath);  
		if(!fileObj.getParentFile().exists()) {
			fileObj.getParentFile().mkdirs();
		}
		if(fileObj.exists()){
			fileObj.delete();
		}
        //保存  
        try {  
        	file.transferTo(fileObj);
		} catch (Exception e) {
			LogMessage.logwrite(this.getClass(), e.getMessage());
		}
		return imageTmpPath ;
	}
		 		 
		 
		 
		
	 

	
}
